// pages/order/order.js
var util = require('../../utils/util.js');
const app = getApp();
var pageNo = 1;
var hasNextPage = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('view.open-type.getUserInfo'),
    baseDomain: app.globalData.baseDomain,
    apiDomain: app.globalData.apiDomain,
    orders: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    if (app.globalData.userInfo) {
      //调用获取订单列表接口
      wx.request({
        url: app.globalData.apiDomain + '/api/order/getOrderByBuyer',
        method: "POST",
        data: {
          openId: app.globalData.openId,
          pageNo: 1
        },
        success: function(res) {
          hasNextPage = res.data.data.hasNextPage;
          that.setData({
            orders: res.data.data.list,
          });
          if (hasNextPage) {
            pageNo = 2;
          }
        },
        complete: function() {

        }
      });
    } else {
      wx.switchTab({
        url: '../userCenter/userCenter',
        complete: function () {
          wx.showToast({
            title: '请先登录',
            image: '../../images/failure_toast.png',
            duration: 2000,
          });
        }
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    //调用获取订单列表接口
    wx.request({
      url: app.globalData.apiDomain + '/api/order/getOrderByBuyer',
      method: "POST",
      data: {
        openId: app.globalData.openId,
        pageNo: 1
      },
      success: function(res) {
        hasNextPage = res.data.data.hasNextPage;
        that.setData({
          orders: res.data.data.list,
        });
        if (hasNextPage) {
          pageNo = 2;
        }
      },
      complete: function() {
        wx.stopPullDownRefresh();
      }
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    //调用获取订单列表接口
    if (hasNextPage) {
      wx.request({
        url: app.globalData.apiDomain + '/api/order/getOrderByBuyer',
        method: "POST",
        data: {
          openId: app.globalData.openId,
          pageNo: pageNo
        },
        success: function(res) {
          hasNextPage = res.data.data.hasNextPage;
          var oldOrders = that.data.orders;
          var list = res.data.data.list;
          for (var i = 0; i < list.length; i++) {
            oldOrders.push(list[i]);
          }
          that.setData({
            orders: oldOrders
          });
          if (hasNextPage) {
            pageNo++;
          }
        },
        complete: function() {
          wx.stopPullDownRefresh();
        }
      });
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  queryExpress: util.throttle(function(e) {
    var postNum = e.currentTarget.dataset.expressNum;
    wx.navigateTo({
      url: '../expressInfo/expressInfo?expressNum=' + postNum,
    })
  }),
  confirmReceive: util.throttle(function(e) {
    var orderId = e.currentTarget.dataset.orderId;
    wx.showModal({
      title: '温馨提示',
      content: '您确认已经收到货了吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: function(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.apiDomain + '/api/order/confirmReceive',
            method: "POST",
            data: {
              id: orderId
            },
            success: function(res) {
              wx.redirectTo({
                url: '../order/order',
                complete: function() {
                  wx.showToast({
                    title: '收货成功',
                    image: '../../images/correct_toast.png',
                    duration: 2000,
                  });
                }
              })
            }
          })
        }
      },
    })
  })
})